import { ElLoading, ElMessage } from "element-plus";
import type { LoadingOptionsResolved } from "element-plus";

let loading: any = null;

const showLoading = (
  option: Partial<LoadingOptionsResolved> = {
    lock: true,
    text: "Loading",
    background: "rgba(0, 0, 0, 0.7)"
  }
) => {
  loading = ElLoading.service(option);
};

const hiddenLoading = () => {
  if (loading && loading.close) {
    loading.close();
  }
};

const message = {
  success(message: string) {
    ElMessage.success({ message, offset: 40 });
  },
  warning(message: string) {
    ElMessage.warning({ message, offset: 40 });
  },
  info(message: string) {
    ElMessage.info({ message, offset: 40 });
  },
  error(message: string) {
    ElMessage.error({ message, offset: 40 });
  }
};

export { showLoading, hiddenLoading, message };
